package cn.agiledata.bank.audit.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.agiledata.bank.audit.exception.AuditException;
import cn.agiledata.bank.audit.form.AuditGroupForm;
import cn.agiledata.bank.audit.service.AuditGroupService;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.hibernateBean.AuditGroup;
import cn.agiledata.bank.hibernateBean.Operator;
import cn.agiledata.bank.myspace.service.IMySpaceService;

import com.vnv.infrastructure.action.IPBBaseAction;
import com.vnv.infrastructure.exception.IPBException;
import com.vnv.infrastructure.logic.IPBAppFactory;

/**
 * @author 马贤波
 * 
 */
public class AuditGroupAction extends IPBBaseAction {

	
	
    private AuditGroupService getAuditGroupService() {
        return (AuditGroupService) IPBAppFactory.getObj("auditGroupService");
    }

    private IMySpaceService getMySpaceService() {
        return (IMySpaceService) IPBAppFactory.getObj("mySpaceService");
    }

    /**
     * 审核组列表
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @return
     * @throws IPBException
     * @throws Exception
     */
    public ActionForward getAuditGroupList(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws IPBException, Exception {
        long accountId = WeatherUtil.getLoingInfoFromSession(request)
                .getAccountId();
        // 判断是否设置过操作员
        if (getMySpaceService().hasOperators(String.valueOf(accountId))) {
            request.setAttribute("hasOperators", Boolean.TRUE);
        } else {
            request.setAttribute("hasOperators", Boolean.FALSE);
        }
        AuditGroup[] auditGroups = getAuditGroupService()
                .getUserAuditGroupList(accountId);
        request.setAttribute("ResultList", auditGroups);
        request.setAttribute("ResultListSize", auditGroups.length + "");
        return mapping.findForward("success");
    }

    /**
     * 添加审核组预备
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @return
     * @throws IPBException
     * @throws Exception
     */
    public ActionForward preAddAuditGroup(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws IPBException, Exception {
        List operators = WeatherUtil.getLoingInfoFromSession(request)
                .getOperatorList();
        request.setAttribute("availableAuditorList", operators);
        request.setAttribute("selectedAuditorList", new Operator[0]);
        return mapping.findForward("success");
    }

    private Operator getCurrentOperator(HttpServletRequest request) {
        Operator operator = WeatherUtil.getOperator(request);
        if (operator == null) {
            List operators = WeatherUtil.getLoingInfoFromSession(request)
                    .getOperatorList();
            operator = (Operator) operators.get(0);
        }
        return operator;
    }

    /**
     * 添加审核组
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @return
     * @throws IPBException
     * @throws Exception
     */
    public ActionForward addAuditGroup(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws IPBException, Exception {
        AuditGroupForm form = (AuditGroupForm) actionForm;
        String name = form.getName();
        String remark = form.getRemark();
        long[] selectedAuditors = form.getSelectedAuditors();
        Operator operator = this.getCurrentOperator(request);
        AuditGroup auditGroup = getAuditGroupService().addUserAuditGroup(
                operator, name, remark, selectedAuditors);
        request.setAttribute("auditors", auditGroup.getOperators());
        return mapping.findForward("success");
    }

    public ActionForward removeAuditGroup(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws IPBException, Exception {
        long auditGroupId = Long.parseLong(request.getParameter("id"));
        if (!getAuditGroupService().removeAuditGroup(auditGroupId)) {
            throw AuditException.AUDIT_GROUP_USED_CANNOT_REMOVE;
        }
        return mapping.findForward("success");
    }

    public ActionForward preAmendAuditGroup(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws IPBException, Exception {
        long id = Long.parseLong(request.getParameter("id"));
        AuditGroup auditGroup = getAuditGroupService().getAuditGroupById(id);
        request.setAttribute("name", auditGroup.getName());
        request.setAttribute("remark", auditGroup.getRemarkNotNull());
        Set selectedAuditors = auditGroup.getOperators();
        request.setAttribute("selectedAuditorList", selectedAuditors);
        request.setAttribute("used", new Boolean(
                Constant.USED_YES == auditGroup.getUsed()));
        List operators = new ArrayList(WeatherUtil.getLoingInfoFromSession(
                request).getOperatorList());
        for (int i = operators.size() - 1; i >= 0; i--) {
            Operator operator = (Operator) operators.get(i);
            for (Iterator it = selectedAuditors.iterator(); it.hasNext();) {
                Operator auditor = (Operator) it.next();
                if (auditor.getId().equals(operator.getId())) {
                    operators.remove(i);
                }
            }
        }
        request.setAttribute("availableAuditorList", operators);
        return mapping.findForward("success");
    }

    public ActionForward amendAuditGroup(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws IPBException, Exception {
        AuditGroupForm form = (AuditGroupForm) actionForm;
        long id = form.getId().longValue();
        String name = form.getName();
        String remark = form.getRemark();
        long[] selectedAuditors = form.getSelectedAuditors();
        Operator operator = this.getCurrentOperator(request);

        AuditGroup auditGroup = getAuditGroupService().amendUserAuditGroup(id,
                operator, name, remark, selectedAuditors);
        request.setAttribute("auditors", auditGroup.getOperators());

        return mapping.findForward("success");
    }
}
